<template>
  <a-form :form='form' ref='baseForm' style='margin-bottom:100px;'>
    <a-spin tip='加载中...' :spinning='spinning'>
      <slot></slot>
    </a-spin>
  </a-form>
</template>
<script>
export default {
  name: "fForm",
  provide() {
    return {
      formParams: this.formParams
    }
  },
  props: {
    action: {
      type: [Object, String],
      default: ""
    },
    handlerName: {
      type: String,
      default: ""
    },
    fileIds: {
      type: Array,
      default: null
    },
    formKey: {
      type: [Number, String],
      default: ""
    }
  },
  data() {
    return {
      callback: null,
      spinning: false,
      formParams: {},
      form: this.$form.createForm(this)
    }
  },
  methods: {
    resetFields() {
      this.form.resetFields()
    },
    resetField(field) {
      this.form.resetFields(field)
    },
    getForm() {
      return this.form
    },
    //设置回调时间
    setCallback(callback) {
      this.callback = callback
    },
    /**
     * 获取数据详情
     * @param params
     */
    getDetails(params, callback) {
      this.spinning = true
      this.http.get(this, {
        url: this.action["get"],
        params: params,
        success: function(me, data) {
          callback && callback(data)
          me.spinning = false
        }
      })
    },
    submit(params, callback) {
      if (callback) {
        this.callback = callback
      }
      if (params.handlerName) {
        this.handlerName = params.handlerName
      }
      this.form.validateFields((valid, values) => {
        if (!valid) {
          console.log("表单自动参数values", values)
          console.log("表单手动参数params", params)
          let postParams = Object.assign({}, params, values)
          let data = {
            id: this.formKey,
            handlerName: this.handlerName,
            fileIds: this.fileIds
          }
          let submitParams = Object.assign({}, data, postParams, this.formParams)
          console.info("提交参数", submitParams)
          this.http.saveOrUpdate(this, {
            api: this.action,
            data: submitParams,
            success: function(me, data) {
              me.callback && me.callback(data)
              //回调事件
              me.$emit("success", data)
            }
          })
        }
      })
    },
    //不带params,后台map接收
    handlerSubmit(params, callback, requestBody) {
      let rb = false
      if (callback) {
        this.callback = callback
      }
      if (requestBody) {
        rb = true
      }
      this.form.validateFields((valid, values) => {
        if (!valid) {
          console.log("表单参数values", values)
          console.log("自定义表单参数params", params)
          let postParams = Object.assign({}, params, values)
          let data = {
            id: this.formKey,
            handlerName: this.handlerName,
            fileIds: this.fileIds
          }
          console.log("this.formParams", this.formParams)
          let submitParams = Object.assign({}, data, postParams, this.formParams)
          console.info("提交参数", submitParams)
          this.http.saveOrUpdate(this, {
            api: this.action,
            data: submitParams,
            requestBody: rb,
            success: function(me, data) {
              me.callback && me.callback(data)
              //回调事件
              me.$emit("success", data)
            }
          })
        }
      })
    }
  },
  beforeDestroy() {
    console.info("form-beforeDestroy")
  },
  computed: {},
  created() {

  }
}
</script>
<style scoped>
.spin-content {
  border: 1px solid #91d5ff;
  background-color: #e6f7ff;
  padding: 30px;
}
</style>